<template>
  <div class="app-container customer-div">
    <el-menu :default-active="type" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item :index="1">整车</el-menu-item>
      <el-menu-item :index="2">散货</el-menu-item>
    </el-menu>
    <div style="margin-top: 40px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="日期：">
          <el-date-picker
            v-model="listQuery.createDate"
            type="daterange"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货地：">
          <el-cascader
            filterable
            clearable
            v-model="listQuery.sendAreaId"
            :props="{ value: 'areaId',label:'areaName',children:'children',emitPath:false ,checkStrictly: true}"
            :options="options"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="收货地：">
          <el-cascader
            filterable
            clearable
            v-model="listQuery.receiveAreaId"
            :props="{ value: 'areaId',label:'areaName',children:'children',emitPath:false ,checkStrictly: true}"
            :options="options"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="牌号：" >
          <el-select v-model="listQuery.sapGoodsId" filterable placeholder="请选择" clearable @change="changeGoods">
            <el-option
              v-for="(item,index) in goodsList"
              :key="index"
              :label="item.goodsSn"
              :value="item.kingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>
          <el-button
            v-if="linePriceListExport"
            :loading="btnLoading"
            @click="downloadExcel"
            type="primary">
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--数据列表-->
    <div class="table-container">
      <el-table
        v-loading="listLoading"
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column
          align="center"
          label="发货地">
          <template slot-scope="scope">
              {{scope.row.sendArea}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="收货地">
          <template slot-scope="scope">
            {{scope.row.receiveArea}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="牌号">
          <template slot-scope="scope">
            {{scope.row.goodsSn}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="type == 1 ? '平均价格' : '价格'">
          <template slot-scope="scope">
              {{type == 1 ? scope.row.averagePrice : scope.row.freightPrice}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="type == 1 ? '是否整车' : '数量'">
          <template slot-scope="scope">
            {{type == 1 ? scope.row.isCompleteCar : scope.row.goodsNum}}
          </template>
        </el-table-column>
        <el-table-column
          :label="type == 1 ? '运输次数' : '询盘备注'"
          align="center">
          <template slot-scope="scope">
            {{type == 1 ? scope.row.priceCount : scope.row.freightRemark}}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {freightList,bulkCargoFreight} from '@/api/allInLine/inquiry'
import {formatDate} from '@/utils/date';
import {getAreaList} from '@/api/allInLine/inquiry'
import store from '@/store'
import { orderGoodsList } from '@/api/partner';
import { mapGetters } from 'vuex'
const defaultListQuery={
  "receiveAreaId" : '', //收货地市ID
  "sendAreaId" : '', //发货地市ID
  "createDate" : [], //日期
  sapGoodsId:'',//牌号
  "pageNum" : 1,
  "pageSize" : 10,
  "isExport" : 0 //是否导出
};

export default {
  name: "linePriceList",
  data(){
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      roles:store.getters.roles ? store.getters.roles : [],
      btnLoading:false,
      options:[],
      type:1,
      goodsList:[]
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
    linePriceListExport(){
      if(this.roles.indexOf("linePriceListExport") != -1){
        return true
      }else{
        return false
      }
    }

  },
  created() {
    let time = new Date();
    let day = time - time % (24 * 60 * 60 * 1000) - 8 * 60 * 60 * 1000;
    let end = new Date(day + 24 * 60 * 60 * 1000-1000);
    let start = new Date(day - 30*24 * 60 * 60 * 1000);
    let startDate = formatDate(start, 'yyyy-MM-dd');
    let endDate = formatDate(end, 'yyyy-MM-dd');
    this.listQuery.createDate = [startDate,endDate]
    this.getList();
    this.getAddressListData()
    orderGoodsList().then((res)=>{
      this.goodsList = res.list
    })
  },
  filters: {

  },

  methods:{
    handleSelect(key){
      this.type = key
      this.handleSearchList()
    },
    // 获取省市区列表
    getAddressListData(){
      getAreaList().then((res)=>{
        // res.forEach((el)=>{
        //   el.children.map(item => {
        //     if(item.children) {
        //       delete item.children
        //     }
        //   })
        // })
        this.options = res;
      })
    },
    //下载excle
    downloadExcel(){
      let params
      params = JSON.parse(JSON.stringify(this.listQuery));
      params.isExport = 1;
      delete params.pageSize;
      delete params.pageNum;
      this.btnLoading = true;
      let fn = freightList
      if(this.type == 2){
        fn = bulkCargoFreight
      }

      fn(params).then((res)=>{
        this.btnLoading = false;
        require.ensure([], () => {
          const { export_json_to_excel } = require('sheet/Export2Excel');
          const tHeader = res.title;
          const filterVal = res.columns;
          const list = res.list;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, res.fileName);
        })
      }).catch(()=>{
        this.btnLoading = false;
      })
    },

    //下载表格数据格式化
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
    },

    //获取列表
    getList() {
      let fn = freightList
      if(this.type == 2){
        fn = bulkCargoFreight
      }
      this.listLoading = true;
      fn(this.listQuery).then(response => {
        this.tableData = response.list;
        this.count = response.count;
        this.listLoading = false;
      }).catch(()=>{
        this.listLoading = false;
      });
    },

  },
}
</script>


